package com.ruoyi.framework.web.service.custom;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.List;

/**
 * 2022-1-18
 */
public class AuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {

    private List<CustomUserDetailService> userDetailsServices;

    @Override
    protected UserDetails retrieveUser(String userName, UsernamePasswordAuthenticationToken authenticationToken) throws AuthenticationException {
       // Map detail =(Map)authenticationToken.getDetails();
        String detail =(String)authenticationToken.getDetails();
        UserDetails userDetails=null;
        for (CustomUserDetailService userDetailService:this.getUserDetailsServices()){
            if(userDetailService.supports(detail)){
                userDetails =userDetailService.loadUserByUsername(userName);
                break;
            }
        }
        if(userDetails ==null){
            try {
                throw new InterruptedException("用户没有呀");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return userDetails;

    }

    /**
     * 密码校验
     * @param userDetails
     * @param authenticationToken
     * @throws AuthenticationException
     */
    @Override
    protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authenticationToken) throws AuthenticationException {
        String detail =(String)authenticationToken.getDetails();
        //pc端验证账号密码
        if(Constants.USER_TYPE_ADMIN.equals(detail)){
            String pas=userDetails.getPassword();
            PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            //authenticationToken.getCredentials()用户输入的密码； pas数据库查询的密码
            if(!passwordEncoder.matches(authenticationToken.getCredentials().toString(),pas)){
                throw  new BadCredentialsException("=====BadCredentialsException");//抛出异常给前面捕获
            }
        }else if(Constants.USER_TYPE_MINI_PASSWORD.equals(detail)){//小程序端账号密码使用
            LoginUser loginUser = (LoginUser)userDetails;
            String pas=loginUser.getMiniUser().getPassword();
            PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            //authenticationToken.getCredentials()用户输入的密码； pas数据库查询的密码
            if(!passwordEncoder.matches(authenticationToken.getCredentials().toString(),pas)){
                throw  new BadCredentialsException("=====BadCredentialsException");//抛出异常给前面捕获
            }
        }
    }

    public List<CustomUserDetailService> getUserDetailsServices() {
        return userDetailsServices;
    }

    public void setUserDetailsServices(List<CustomUserDetailService> userDetailsServices) {
        this.userDetailsServices = userDetailsServices;
    }


}
